<?php

namespace App\Http\Middleware;

use App\Helpers\ApiResponse;
use Closure;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Tymon\JWTAuth\Facades\JWTAuth;

class VerifyUserId
{
    use ApiResponse;

    /**
     * 验证登录
     * @param $request
     * @param Closure $next
     * @return JsonResponse|mixed
     */
    public function handle(Request $request, Closure $next)
    {
        $request->uid = 0;
        $token = $request->header('Authorization');
        if (!empty($token) && $user = JWTAuth::parseToken()->authenticate()) {
            // 注入Request
            $request->uid = $user->id;
        }

        return $next($request);
    }
}
